<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <banner-com></banner-com>
      <hr />
      <list-com></list-com>
    </div>
    <template id="list">
      <div>
        <h1>list组件</h1>
        <button @click="send">传送数据</button>
      </div>
    </template>
    <template id="banner">
      <div>
        <h1>banner组件</h1>
        <h1>{{jdata}}</h1>
      </div>
    </template>
    <script src="vue.js"></script>
    <script>
      // bus传值 完成组件之间传值
      //通过空实例完成传值
      Vue.prototype.$bus = new Vue({})
      // var bus = new Vue()
      var vm = new Vue({
        el: '#app',
        data() {
          return {}
        },
        components: {
          listCom: {
            template: '#list',
            data() {
              return {
                listdata: '你好'
              }
            },
            methods: {
              send() {
                // bus触发事件
                console.log(1111)
                this.$bus.$emit('sdata', this.listdata)
              }
            },
            created() {
              // bus触发事件
              console.log(1111)
              this.$bus.$emit('sdata', this.listdata)
            },
            mounted() {
              // // bus触发事件
              // console.log(1111)
              // this.$bus.$emit('sdata', this.listdata)
            }
          },
          bannerCom: {
            template: '#banner',
            data() {
              return {
                jdata: ''
              }
            },
            created() {
              // 监控自定义事件的触发
              console.log(222)
              this.$bus.$on('sdata', (a, b) => {
                console.log('888888' + a)
                this.jdata = a
              })
            },
            mounted() {}
          }
        }
      })
    </script>
  </body>
</html>
